//
//  SectionListViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/8/6.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "SectionListViewController.h"

@interface SectionListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation SectionListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"section"];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self getData];
}
- (IBAction)cancelBtn:(UIButton *)sender {
     [self.popupController dismiss];
}
#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"section" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    LeaderModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.name;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     LeaderModel *model = self.dataArray[indexPath.row];
    if (self.sectionBlock) {
        self.sectionBlock(model.name, model.id);
        [self.popupController dismiss];
    }
}

-(void)getData{
    [[ApiManager ShardInstance]getSectionsstaff_id:[TokenManager getUserId] resultBlock:^(NSDictionary *data, NSError *error) {
        self.dataArray = [LeaderModel mj_objectArrayWithKeyValuesArray:data[@"rs"]];
        [self.tableView reloadData];
    }];
}

@end
